﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CE.Database;
using System.Collections;

public partial class MasterPage : System.Web.UI.MasterPage
{
    string selectedLanguage;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/SiteUserSecureLogin.aspx");
        }

        if (Request.UserAgent.IndexOf("AppleWebKit") > 0)
        {
            Request.Browser.Adapters.Clear();
        }

        #region Page Script Reference
        HyperLink _hplJS = new HyperLink();
        _hplJS.NavigateUrl = "~/css/main.css";
        Page.RegisterClientScriptBlock("MyScriptCss", "<link rel='stylesheet' type='text/css' href='" + _hplJS.ResolveClientUrl(_hplJS.NavigateUrl) + "'/>");

        _hplJS.NavigateUrl = "~/css/colorbox.css";
        Page.RegisterClientScriptBlock("MyScriptColorBoxCss", "<link rel='stylesheet' type='text/css' href='" + _hplJS.ResolveClientUrl(_hplJS.NavigateUrl) + "'/>");

        _hplJS.NavigateUrl = "~/css/print.css";
        Page.RegisterClientScriptBlock("MyScriptCss2", "<link rel='stylesheet' type='text/css' href='" + _hplJS.ResolveClientUrl(_hplJS.NavigateUrl) + "' media='print'/>");

        _hplJS.NavigateUrl = "~/js/datatables/css/jquery.dataTables.css";
        Page.RegisterClientScriptBlock("MyCss", "<link rel='stylesheet' type='text/css' href='" + _hplJS.ResolveClientUrl(_hplJS.NavigateUrl) + "'/>");

        _hplJS.NavigateUrl = "~/favicon.png";
        Page.RegisterClientScriptBlock("MyScriptFavicon", "<link rel='shortcut icon' href='" + _hplJS.ResolveClientUrl(_hplJS.NavigateUrl) + "'/>");

        _hplJS.NavigateUrl = "~/js/jquery.min.js";
        Page.RegisterClientScriptBlock("MyQueryMIN", "<script type='text/javascript' src='" + _hplJS.ResolveClientUrl(_hplJS.NavigateUrl) + "'></script>");

        _hplJS.NavigateUrl = "~/js/colorbox/jquery.colorbox-min.js";
        Page.RegisterClientScriptBlock("MyQueryColorBox", "<script type='text/javascript' src='" + _hplJS.ResolveClientUrl(_hplJS.NavigateUrl) + "'></script>");

        _hplJS.NavigateUrl = "~/js/jquery-ui.min.js";
        Page.RegisterClientScriptBlock("MyQueryUIMIN", "<script type='text/javascript' src='" + _hplJS.ResolveClientUrl(_hplJS.NavigateUrl) + "'></script>");

        _hplJS.NavigateUrl = "~/carinsurance/renewal/magnific-popup/jquery.magnific-popup.js";
        Page.RegisterClientScriptBlock("MagnificPop", "<script type='text/javascript' src='" + _hplJS.ResolveClientUrl(_hplJS.NavigateUrl) + "'></script>");

        _hplJS.NavigateUrl = "~/js/plugins/ui/jquery.collapsible.min.js";
        ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "MyScript12", "<script type='text/javascript' src='" + _hplJS.ResolveClientUrl(_hplJS.NavigateUrl) + "'></script>",false);

        //_hplJS.NavigateUrl = "~/js/superbox/jquery.superbox.css";
        //Page.RegisterClientScriptBlock("MySuperBoxCss", "<link rel='stylesheet' type='text/css' href='" + _hplJS.ResolveClientUrl(_hplJS.NavigateUrl) + "'/>");

        //_hplJS.NavigateUrl = "~/js/superbox/jquery.superbox-min.js";
        //Page.RegisterClientScriptBlock("MySuperBoxMin", "<script type='text/javascript' src='" + _hplJS.ResolveClientUrl(_hplJS.NavigateUrl) + "'></script>");

        //_hplJS.NavigateUrl = "~/js/superbox/jquery.superbox.js";
        //Page.RegisterClientScriptBlock("MySuperBox", "<script type='text/javascript' src='" + _hplJS.ResolveClientUrl(_hplJS.NavigateUrl) + "'></script>");

        //_hplJS.NavigateUrl = "~/js/datatables/jquery.dataTables.js";
        //Page.RegisterClientScriptBlock("MyDataTable", "<script type='text/javascript' src='" + _hplJS.ResolveClientUrl(_hplJS.NavigateUrl) + "'></script>");

        #endregion

        if (!Page.IsPostBack)
        {
            btnLanguage.OnClientClick = "javascript:return confirm('" + Resources.CarInsurance.LanguageChangeMessage + "');";

            string _strCXLive = CE.Configuration.MyConfigurationManager.GetAppSettingValue("CXCRMIsCXLive");
            if (_strCXLive == "0")
            {
                if (Session[CE.Global.General.Guest] == null)
                {
                    Response.Redirect("~/Default.aspx");
                }
            }

            if (Session[CE.Global.General.UserId] != null)
            {
                Page.Title = "GG-CRM - กล้วยๆ.com - Teledirect Thailand";
                lblLogInOut.Text = Resources.General.SignOut;
                hplChangePassword.Visible = true;
                hplLogInOut.NavigateUrl = "~/User/Logout.aspx";
                hplLogInOut.Attributes.Add("onclick", "javascript:return confirm('" + Resources.General.ConfirmSIGNOUTMessage + "')");
            }

            #region Dashboard
            DateTime time = CE.Global.General.GetUTCDateTime();
            lblTodayDateSmall.Text += "Dashboard (" + time.ToString("dddd MMMM dd, yyyy") + ")";
            lblTodayDateSmall.Font.Bold = true;

            string _ciPendingCallBackCount = GetPendingCallBackCount();
            lblSideCIPendingCallCount.Text = "<strong>" + _ciPendingCallBackCount + "</strong>";
            Int32 _Pending = 0;
            if (_ciPendingCallBackCount != string.Empty)
            {
                if (!Int32.TryParse(_ciPendingCallBackCount, out _Pending))
                {
                    _Pending = 0;
                }
            }

            string _ciNormalCountbyAgent = GetCarDetailCountByAgent(Convert.ToInt64(Session[CE.Global.General.UserId].ToString()));
            Int32 _NewLead = 0;
            if (_ciNormalCountbyAgent != string.Empty)
            {
                if (!Int32.TryParse(_ciNormalCountbyAgent, out _NewLead))
                {
                    _NewLead = 0;
                }
            }

            //if (_NewLead > 0)
            //{
            //    lblSideCICallMeCount.Text = "<strong>" + _NewLead.ToString() + "</strong>";
            //}


            Int32 TotalPending = 0;
            TotalPending = _Pending + _NewLead;
            // 03May2014
            //string _ciCallMeCount = GetCarDetailCount(101, Convert.ToInt64(Session[CE.Global.General.UserId].ToString()));
            //int _intCallMeCount = 0;
            //if (int.TryParse(_ciCallMeCount.Split(' ')[0].ToString(), out _intCallMeCount))
            //{
            //    if (_intCallMeCount > 0)
            //    {
            //        hplSideCICallMe.BorderColor = System.Drawing.Color.Yellow;
            //    }
            //}

            int _PendingCallBackCount = 0;
            if (TotalPending > 0)
            {
                //PHYO
                lblSideCINormalCallCount.Text = "<strong>" + TotalPending.ToString() + "</strong>";
                if (Int32.TryParse(_ciPendingCallBackCount, out _PendingCallBackCount))
                {
                    if (_PendingCallBackCount > 0)
                    {
                        hplSideCINormalCall.BorderColor = System.Drawing.Color.Red;
                        //Timer1.Interval += 60 * 5 * 1000;
                    }
                }
            }

            if (Session[CE.Global.General.UserRole] != null)
            {
                if (Session[CE.Global.General.UserRole].ToString() == "201")
                {
                    hplCINormal.Enabled = false;
                    hplDashbook.Enabled = false;
                    hplFeedBack.Enabled = false;

                    hplSideCICallDirect.Visible = false;
                    //hplSideCICallMe.Visible = false;
                    hplSideCINormalCall.Visible = false;
                    hplPendingCall.Visible = false;

                    lblCICallMeBackCount.Visible = false;
                    lblCICallMeSmallCount.Visible = false;
                    lblCINormalSmallCount.Visible = false;

                    hplSearchCustomer.NavigateUrl = "~/carinsurance/ViewCICustomerCallNewSearch.aspx?page=oldquote";
                }

                //if (Session[CE.Global.General.UserRole].ToString() != "100")
                //{
                //    Timer1.Interval = 24 * 60 * 60 * 1000;
                //    Timer1.Enabled = false;
                //}
                //else
                //{
                //    if (_PendingCallBackCount > 0)
                //    {
                //        if (Request.Url.PathAndQuery.ToLower().Contains("siteuserhome.aspx"))
                //        {
                //        }
                //    }
                //}
            }

            //[Yui] @ 08/08/2013 
            if (Session[CE.Global.General.IsRenewalAgent].ToString() == "1")
            {
                hplRenewal.Visible = true;
            }

            #endregion
        }
    }

    private string GetCarDetailCount(int intCallType, Int64 AgentID)
    {
        string _retValue = "0";
        try
        {
            //[sprocCXCRMGetCarDetailCount]
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocCXCRMGetCarDetailCount";
            SortedList _paraList = new SortedList();
            _paraList.Add("@CallType", intCallType);
            _paraList.Add("@LangOption", lblLangOption.Text);
            _paraList.Add("@AgentID", AgentID);
            object _objRetCount = _db.ExecuteScalarSP(_spName, _paraList);
            if (_objRetCount != null)
            {
                _retValue = _objRetCount.ToString();
            }
        }
        catch (Exception Ex)
        {
            _retValue = "N/A";
        }
        return _retValue;
    }

    private string GetCarDetailCountByAgent(Int64 AgentId)
    {
        string _retValue = "0";
        try
        {
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocCXCRMGetCarDetailCountByAgentId";
            SortedList _paraList = new SortedList();
            _paraList.Add("@AgentId", AgentId);
            object _objRetCount = _db.ExecuteScalarSP(_spName, _paraList);
            if (_objRetCount != null)
            {
                _retValue = _objRetCount.ToString();
            }
        }
        catch (Exception Ex)
        {
            _retValue = "N/A";
        }
        return _retValue;
    }

    private string GetPendingCallBackCount()
    {
        string _retValue = "0";
        try
        {
            if (Session[CE.Global.General.UserId] != null)
            {
                MyDatabase _db = new MyDatabase();
                string _spName = "sprocCXCRMGetPendingCallBackCount";
                SortedList _paraList = new SortedList();
                _paraList.Add("@AgentId", Session[CE.Global.General.UserId].ToString());
                object _objRetCount = _db.ExecuteScalarSP(_spName, _paraList);
                if (_objRetCount != null)
                {
                    _retValue = _objRetCount.ToString();
                }
            }
        }
        catch (Exception Ex)
        {
        }
        return _retValue;
    }

    protected void btnLanguage_Click(object sender, EventArgs e)
    {
        selectedLanguage = string.Empty;

        if (lblLanguage.Text != string.Empty)
        {
            Session["Language"] = lblLanguage.Text;
        }

        if (Session["Language"] != null)
        {
            selectedLanguage = Session["Language"].ToString();
        }
        else
        {
            selectedLanguage = "th-TH";
            Session["Language"] = selectedLanguage;
        }

        if (selectedLanguage == "th-TH")
        {
            Session["Language"] = "en-US";
        }
        else
        {
            Session["Language"] = "th-TH";
        }

        lblLanguage.Text = selectedLanguage;

        Response.Redirect(Request.UrlReferrer.AbsoluteUri);
    }

    protected void UpdatePendingStatus(object sender, EventArgs e)
    {
        try
        {
            string _ciStrCallBackCount = GetPendingCallBackCount();
            int _ciIntCallBackCount = 0;
            if (Int32.TryParse(_ciStrCallBackCount, out _ciIntCallBackCount))
            {
                if (_ciIntCallBackCount > 0)
                {
                    hplSideCINormalCall.BorderColor = System.Drawing.Color.Red;
                    ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), Guid.NewGuid().ToString(), "alert('[หมดเวลา!]โปรดตรวจสอบตารางเวลาการโทรกลับ.\\nคุณมีตารางโทรกลับ  " + _ciIntCallBackCount.ToString() + " รายการ\\n\\n[Time Up!] Please check call back schedule.\\nYou got (" + _ciIntCallBackCount.ToString() + ") call back schedule(s) due.');", true);
                    //Timer1.Interval += 60 * 5 * 1000;
                }
            }
        }
        catch (Exception Ex)
        {
            Ex.ToString();
        }
    }

}
